/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
***/
package stripesbook.ext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.stripes.action.ErrorResolution;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.FileUploadLimitExceededException;
import net.sourceforge.stripes.exception.ActionBeanNotFoundException;
import net.sourceforge.stripes.exception.DefaultExceptionHandler;
import net.sourceforge.stripes.util.Log;
import stripesbook.action.user.MessageComposeActionBean;

public class MyExceptionHandler extends DefaultExceptionHandler {
    private static final String VIEW = "/view/error.vm";
    private static final Log log =
        Log.getInstance(MyExceptionHandler.class);

    public Resolution catchAttachmentsTooBig(
        FileUploadLimitExceededException exc,
        HttpServletRequest req, HttpServletResponse resp)
    {
        return new RedirectResolution(MessageComposeActionBean.class,
            "recover")
            .addParameter("maximumSize", exc.getMaximum())
            .addParameter("postedSize", exc.getPosted());
    }
    public Resolution catchActionBeanNotFound(
        ActionBeanNotFoundException exc,
        HttpServletRequest req, HttpServletResponse resp)
    {
        return new ErrorResolution(HttpServletResponse.SC_NOT_FOUND);
    }
    public Resolution catchAll(Throwable exc, HttpServletRequest req,
        HttpServletResponse resp)
    {
        log.error(exc);
        return new ForwardResolution(VIEW);
    }
}
